﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using SouthernLand.Common;
using SouthernLand.Common.BOL.NhaDat;
using SouthernLand.Common.Untilities;

namespace SouthernLand.Controls.NhaDat
{
    public partial class AdvanceSearch : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Load_ddlQuanHuyen();
            }

            Pager.IndexChanged += PageIndex_Changed;
        }

        protected void dlTinhThanh_SelectedIndexChanged(object sender, EventArgs e)
        {
            Load_ddlQuanHuyen();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            rptRaovat.ItemDataBound += new RepeaterItemEventHandler(rptRaovat_ItemDataBound);
            btSearch.Click += new EventHandler(btSearch_Click);
        }

        void btSearch_Click(object sender, EventArgs e)
        {
            DisplayPager.Visible = true;
            DataBind_Raovat();
        }

        void rptRaovat_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Raovat raovat = (Raovat)e.Item.DataItem;
                if (raovat != null)
                {
                    HyperLink hplImageView = (HyperLink)e.Item.FindControl("hplImageView");
                    Image imgReal = (Image)e.Item.FindControl("imgReal");
                    hplImageView.NavigateUrl = Globals.BuildUrlByRealEstateDetails(raovat.RaovatID, Globals.ProcessString(Globals.UnicodeToKoDau(raovat.Tieude)));
                    imgReal.ImageUrl = Globals.ApplicationPath + "Raovat/Image.ashx?Size=S&ImageUrl=" + raovat.Hinh1 +
                                       "&LoaitinID=" + raovat.Loaitin;
                    imgReal.Attributes.Add("onmouseover",
                                           "showtrail('" + Globals.ApplicationPath +
                                           "Raovat/Image.ashx?Size=L&ImageUrl=" + raovat.Hinh1 + "&LoaitinID=" +
                                           raovat.Loaitin + "','" + raovat.Tieude + "',500,500)");
                    imgReal.Attributes.Add("onmouseout", "hidetrail();");
                    Label lblTotalViews = (Label)e.Item.FindControl("lblTotalViews");
                    lblTotalViews.Text = raovat.TotalViews.ToString();
                    HyperLink hplRealEstateTitle = (HyperLink)e.Item.FindControl("hplRealEstateTitle");
                    hplRealEstateTitle.Text = raovat.Tieude;
                    hplRealEstateTitle.NavigateUrl = Globals.BuildUrlByRealEstateDetails(raovat.RaovatID, Globals.ProcessString(Globals.UnicodeToKoDau(raovat.Tieude)));
                }
            }
        }

        private void Load_ddlQuanHuyen()
        {
            List<QuanHuyen> obj =
                QuanHuyen.GetQuanHuyenByTinhThanhID(ConvertUtility.ToInt32(dlTinhthanh.SelectedItem.Value));
            dlQuanHuyen.DataSource = obj;
            dlQuanHuyen.DataValueField = "QuanHuyenID";
            dlQuanHuyen.DataTextField = "TenQuanHuyen";
            dlQuanHuyen.DataBind();
            dlQuanHuyen.Items.Insert(0, new ListItem(ResourceManager.GetString("PleaseSelect"), ""));
        }

        private void PageIndex_Changed(Object sender, EventArgs e)
        {
            DataBind_Raovat();
        }

        public void DataBind_Raovat()
        {
            var obj = new Raovat();
            obj.Loaitin = ConvertUtility.ToInt32(dlLoaitin.SelectedItem.Value);
            obj.CateID = ConvertUtility.ToInt32(dlLoaiBDS.SelectedItem.Value);
            obj.Tinhthanh = ConvertUtility.ToInt32(dlTinhthanh.SelectedItem.Value);
            obj.Quanhuyen = ConvertUtility.ToInt32(dlQuanHuyen.SelectedItem.Value);
            obj.HuongNha = ConvertUtility.ToInt32(dlHuongNha.SelectedItem.Value);
            obj.TinhtrangPL = ConvertUtility.ToInt32(dlTinhTrangPL.SelectedItem.Value);

            //(Gia < 1)|2 (2: don vi tinh: trieu, ty...)
            string strWhereGia = "";
            int intDonVT = 2; // trieu vnd
            if (dlGia.SelectedItem.Value != "")
            {
                string strtempGia = dlGia.SelectedItem.Value;
                strWhereGia = strtempGia.Substring(0, strtempGia.LastIndexOf("|")).Trim();
                intDonVT = ConvertUtility.ToInt32(strtempGia.Substring(strtempGia.LastIndexOf("|") + 1, 1));
            }

            string strWhereDientichDat = !string.IsNullOrEmpty(dlTongDienTich.SelectedItem.Value)
                                             ? dlTongDienTich.SelectedItem.Value
                                             : string.Empty;

            Pager.PageSize = 10;
            List<Raovat> lSearch = Raovat.Search(eTrangthai.HieuLuc, "", obj, strWhereGia, intDonVT, strWhereDientichDat,
                                                 "RaovatID DESC", Pager.PageIndex, Pager.PageSize);
            rptRaovat.DataSource = lSearch;
            rptRaovat.DataBind();

            int TotalRecords = 0;
            foreach (Raovat f in lSearch)
            {
                TotalRecords = f.TotalRecords;
                break;
            }

            DisplayPager.Visible = TotalRecords > Pager.PageSize;
            Pager.TotalRecords = CurrentPage.TotalRecords = TotalRecords;
            PanelMsg.Visible = true;
            lblMsg.Text = lSearch.Count > 0
                              ? "Tìm được: <span style='color:red'>" + Pager.TotalRecords + "</span> Kết quả "
                              : "Không tìm thấy kết quả nào theo yêu cầu bạn chọn.";
        }
    }
}